package com.hzhr.iot.statis.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygQuery;
import com.hzhr.iot.core.annotation.CygQuerys;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.constant.DeviceOptions;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.options.OptionsModel;
import com.hzhr.iot.device.sdk.service.Device_OptionsService;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import com.hzhr.iot.statis.convertor.custom.DeviceDataExceptionCustomConvertor;
import com.hzhr.iot.statis.dao.DeviceDataExceptionDao;
import com.hzhr.iot.statis.entity.DeviceDataExceptionEntity;
import com.hzhr.iot.statis.entity.ext.DeviceDataExceptionEntityExt;
import com.hzhr.iot.statis.model.deviceDataException.DeviceDataExceptionGetModel;
import com.hzhr.iot.statis.model.deviceDataException.DeviceDataExceptionModel;
import com.hzhr.iot.statis.queryModel.custom.DeviceDataExceptionCustomQueryModel;
import com.hzhr.iot.statis.service.base.BaseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

@Service
@CygService(entity = DeviceDataExceptionEntity.class, model = DeviceDataExceptionModel.class)
@CygQuerys({
        @CygQuery(name = "_equal_exceptionType", query = "equal_exceptionType", type = String.class),
        @CygQuery(name = "_greaterthanorequal_exceptionDayCount", query = "greaterthanorequal_exceptionDayCount", type = Integer.class),
        @CygQuery(name = "_greaterthanorequal_statisDate", query = "greaterthanorequal_statisDate", type = Date.class),
        @CygQuery(name = "_lessthan_statisDate", query = "lessthan_statisDate", type = Date.class),
        @CygQuery(name = "_equal_statisDate", query = "equal_statisDate", type = Date.class),
        @CygQuery(name = "_equal_collectDeviceCategoryCode", query = "equal_collectDeviceCategoryCode", type = String.class),
        @CygQuery(name = "_equal_collectDeviceId", query = "equal_collectDeviceId", type = String.class),


})
public class DeviceDataExceptionService extends BaseService {
    @Autowired
    DeviceDataExceptionCustomConvertor deviceDataExceptionCustomConvertor;

    @Autowired
    DeviceDataExceptionDao deviceDataExceptionDao;

    @Autowired
    Device_OptionsService device_optionsService;


    //region javadoc注释 : get
    /**
     * 查询登录用户管辖范围内的设备数据异常 <br/><br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;根据条件参数查询设备数据异常(默认按统计时间倒序、异常发生天数倒序排列) <br/>
     * 2.&nbsp;返回信息 <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _is_paging : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否分页;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_exceptionType : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceDataExceptionModel.exceptionType = #{_equal_exceptionType};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_collectDeviceId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceDataExceptionModel.collectDeviceId = #{_equal_collectDeviceId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _greaterthanorequal_exceptionDayCount : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceDataExceptionModel.exceptionDayCount >= #{_greaterthanorequal_exceptionDayCount};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _greaterthanorequal_statisDate : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceDataExceptionModel.statisDate >= #{_greaterthanorequal_statisDate};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _lessthan_statisDate : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceDataExceptionModel.statisDate < #{_greaterthanorequal_statisDate};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_statisDate : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceDataExceptionModel.statisDate = #{_equal_statisDate};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _equal_collectDeviceCategoryCode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where DeviceDataExceptionModel.collectDeviceCategoryCode = #{_equal_collectDeviceCategoryCode};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/><br/>
     * @throws ServiceException <br/>
     * @see DeviceDataExceptionGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询登录用户管辖范围内的设备数据异常
    //步骤 :
    //1. 根据条件参数查询设备数据异常(默认按统计时间倒序、异常发生天数倒序排列)
    //2. 返回信息
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "is_paging", value = "是否分页", defaultValue = "true",required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_exceptionType", value = "筛选条件 : where DeviceDataExceptionModel.exceptionType = #{equal_exceptionType}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_collectDeviceId", value = "筛选条件 : where DeviceDataExceptionModel.collectDeviceId = #{collectDeviceId}",required = false, dataType = "int", paramType = "query"),

            @ApiImplicitParam(name = "greaterthanorequal_exceptionDayCount", value = "筛选条件 : where DeviceDataExceptionModel.exceptionDayCount >= #{greaterthanorequal_exceptionDayCount}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "greaterthanorequal_statisDate", value = "筛选条件 : where DeviceDataExceptionModel.statisDate >= #{greaterthanorequal_statisDate}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "lessthan_statisDate", value = "筛选条件 : where DeviceDataExceptionModel.statisDate < #{greaterthanorequal_statisDate}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_statisDate", value = "筛选条件 : where DeviceDataExceptionModel.statisDate = #{equal_statisDate}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_collectDeviceCategoryCode", value = "筛选条件 : where DeviceDataExceptionModel.collectDeviceCategoryCode = #{equal_collectDeviceCategoryCode}",required = false, dataType = "string", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_is_paging", value = "是否分页", defaultValue = "true",required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_exceptionType", value = "筛选条件 : where DeviceDataExceptionModel.exceptionType = #{_equal_exceptionType}",required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_collectDeviceId", value = "筛选条件 : where DeviceDataExceptionModel.collectDeviceId = #{_equal_collectDeviceId}",required = false, dataType = "int", paramType = "query"),

            @ApiImplicitParam(name = "_greaterthanorequal_exceptionDayCount", value = "筛选条件 : where DeviceDataExceptionModel.exceptionDayCount >= #{_greaterthanorequal_exceptionDayCount}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_greaterthanorequal_statisDate", value = "筛选条件 : where DeviceDataExceptionModel.statisDate >= #{_greaterthanorequal_statisDate}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_lessthan_statisDate", value = "筛选条件 : where DeviceDataExceptionModel.statisDate < #{_greaterthanorequal_statisDate}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_statisDate", value = "筛选条件 : where DeviceDataExceptionModel.statisDate = #{_equal_statisDate}",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_collectDeviceCategoryCode", value = "筛选条件 : where DeviceDataExceptionModel.collectDeviceCategoryCode = #{_equal_collectDeviceCategoryCode}",required = false, dataType = "string", paramType = "query"),

    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/device-data-exceptions", method = RequestMethod.GET)
    //endregion
    public DeviceDataExceptionGetModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        /* 根据条件参数查询设备数据异常 */
        UserCardModel userCard = getUserCard(header);
        DeviceDataExceptionCustomQueryModel queryModel = deviceDataExceptionCustomConvertor.deviceDataExceptionService_query2queryModel(query);
        queryModel.setLike_collectDeviceResourceCode(userCard.getUserCode() + "%");
        List<String> custom = new ArrayList<>();
        custom.add("desc");
        custom.add("desc");
        queryModel.setCustom1(custom);
        List<DeviceDataExceptionEntityExt> extList = null;
        String show = HzhrTypeUtil.object2string(query.get("_show"));
        if (!StringUtils.isEmpty(show)) {
            extList = deviceDataExceptionDao.selectExtList(queryModel);
        }
        Integer total = null;
        Boolean showTotal = HzhrTypeUtil.object2boolean(query.get("_show_total"));
        if (showTotal != null && showTotal) {
            total = deviceDataExceptionDao.selectCount(queryModel);
        }

        /* 返回信息 */
        DeviceDataExceptionGetModel result = deviceDataExceptionCustomConvertor.extList2deviceDataExceptionGetModel(extList, total, null, query);
        //写入模型的collectDeviceCategoryDes属性
        Map<String, Object> query1 = new HashMap<>();
        String typeName = "COLLECT_DEVICE_CATEGORY";
        query1.put("show", "list");
        query1.put("show_info", true);
        query1.put("equal_typeName", typeName);
        List<OptionsModel> optionsModels = device_optionsService.get(initHeader(header, AccessModeEnum.GLOBAL), query1).getModelList();
        if ("one".equals(show)) {
            DeviceDataExceptionModel model = result.getModel();
            if (model != null) {
                String categoryCode = model.getCollectDeviceCategoryCode();
                for (OptionsModel optionsModel:
                optionsModels) {
                    if (categoryCode.equals(optionsModel.getCode())) {
                        model.setCollectDeviceCategoryDes(optionsModel.getDes());
                        break;
                    }
                }
            }
        }
        if ("list".equals(show)) {
            List<DeviceDataExceptionModel> models = result.getModelList();
            if (models != null) {
                for (DeviceDataExceptionModel model :
                models) {
                    for (OptionsModel optionsModel:
                            optionsModels) {
                        String categoryCode = model.getCollectDeviceCategoryCode();
                        if (categoryCode.equals(optionsModel.getCode())) {
                            model.setCollectDeviceCategoryDes(optionsModel.getDes());
                            break;
                        }
                    }
                }
            }
        }
        return result;
    }

}





































































































































































































































































































































































































































